package com.ztgf.commodity.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.common.config.YmlConfig;
import com.ztgf.commodity.mapper.CommodityUnitMapper;
import com.ztgf.commodity.models.bo.CommodityUnitSaveBO;
import com.ztgf.commodity.models.dto.CommodityUnitDTO;
import com.ztgf.commodity.models.service.ICommodityUnitService;
import com.ztgf.commodity.models.vo.CommodityCategoryUnitVO;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * author: chenyu.wang
 * date: 2020-05-26 11:58
 * Description:
 */
@DubboService
public class CommodityUnitServiceImpl extends ServiceImpl<CommodityUnitMapper, CommodityUnitDTO> implements ICommodityUnitService {

    @Resource
    private YmlConfig ymlConfig;

    @Override
    public Result<List<String>> add(CommodityUnitSaveBO saveBO) {
        Result<List<String>> result = new Result<>();
        CommodityUnitDTO dto = new CommodityUnitDTO();
        //查询判断是否有相同名称
        Wrapper<CommodityUnitDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("code", saveBO.getCode());
        List<CommodityUnitDTO> unitDTOS = this.selectList(wrapper);
        if (unitDTOS != null && unitDTOS.size() > 0) {
            result.setCode(100001);
            result.setMsg("已有相同名称！");
            return result;
        }
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(ymlConfig.getPkId());
        boolean insert = this.insert(dto);
        if (insert) {
            result.setCode(CodeEnum.SUCCESS.getCode());
            result.setMsg(CodeEnum.SUCCESS.getMsg());
            return result;
        }
        result.setCode(100001);
        result.setMsg("操作失败！");
        return result;
    }

    @Override
    public boolean deleteLogic(Long id) {
        CommodityUnitDTO dto = this.selectById(id);
        if (dto != null) {
            if (dto.getStatus().equals(0)) {
                dto.setStatus(1);
            } else {
                dto.setStatus(0);
            }
            return this.updateById(dto);
        }
        return false;
    }

    @Override
    public List<CommodityCategoryUnitVO> queryCategoryUnit(Long categoryId, Long brandId) {
        return baseMapper.queryCategoryUnit(categoryId, brandId);
    }

    @Override
    public String getUnitids() {
        return baseMapper.getUnitids();
    }
}
